import { createRouter, createWebHashHistory } from 'vue-router'
const routes = [
  {
    path: '/',
    component: () => import('../views/nav-view.vue'),
    children: [
      {
        path: '/',
        redirect: '/map',
        component: () => import('../views/Home/map-view.vue'),
      },
      { path: 'map', component: () => import('../views/Home/map-view.vue') },
      {
        path: 'report',
        component: () => import('../views/Home/report-view.vue'),
      },
      { path: 'user', component: () => import('../views/Home/user-view.vue') },
    ],
  },
  { path: '/pictest', component: () => import('../components/PicView.vue') },
  { path: '/noticelist', component: () => import('../views/notice-list.vue') },
  { path: '/eventlist', component: () => import('../views/event-list.vue') },
  { path: '/login', component: () => import('../views/login-view') },
  { path: '/threed', component: () => import('../views/three-d') },
  { path: '/enrole', component: () => import('../views/enrole-view') },
  { path: '/:error*', component: () => import('../views/404.vue') },
]
const router = createRouter({
  // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
  history: createWebHashHistory(),
  routes, // `routes: routes` 的缩写
})

router.beforeEach((to, from, next) => {
  // to: 到哪个path去
  // from: 从哪个path来
  // next(): 是否可以通行
  
  let isLogin = localStorage.getItem('isLogin') === '1' ? true : false
  if(to.path == '/login' ||to.path == '/enrole') {
    next()
    return
  }
  if(isLogin){
    next()
    return
  } next('./login')
  
//   if (isLogin == false) {
//     // 未登录, 只能访问/login
//     if (to.path != '/login' && to.path != '/enrole') {
//       alert('请先登录')
//       next('/login')
//     } else {
//       next()
//     }
//   }
})
export default router
